package com.micookie.v2ex.ui;

import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import com.micookie.v2ex.R;
import com.micookie.v2ex.model.bean.Node;
import com.micookie.v2ex.ui.base.BaseSwipeBackActivity;
import com.micookie.v2ex.ui.fragment.TopicListFragment;
import com.micookie.v2ex.utils.ToastUtils;

import butterknife.BindView;

public class NodeTopicListActivity extends BaseSwipeBackActivity {
    public static final String KEY_NODE_INFO = "KEY_NODE_INFO";

    @BindView(R.id.toolbar_single_node_list)
    Toolbar mToolbar;

    Fragment mTopicListFragment;

    Node mNode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_node_topic_list);
    }

    @Override
    public void initData() {
        super.initData();
        mNode = getIntent().getParcelableExtra(KEY_NODE_INFO);
    }

    @Override
    public void initView() {
        super.initView();
        setSupportActionBar(mToolbar);
        mToolbar.setTitle(mNode.getTitle());
    }

    @Override
    public void initListener() {
        super.initListener();


        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                NodeTopicListActivity.this.finish();
            }
        });

        mToolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                return false;
            }
        });
    }

    @Override
    public void initLoad() {
        super.initLoad();
        showTopicListFragment();
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.node_topic_list_menu, menu);
        return true;
    }

    private void showTopicListFragment() {
        FragmentTransaction transaction = getFragmentManager().beginTransaction();
        if (mTopicListFragment == null) {
            mTopicListFragment = TopicListFragment.startFragment(mNode.getUrl());
            transaction.add(R.id.single_node_list_frame, mTopicListFragment);
        }
        //hideFragment();
        transaction.show(mTopicListFragment);
        transaction.commit();

    }

    public static void startActivity(Context context, Node node) {
        if (node != null && node.getUrl() != null && !node.getUrl().isEmpty()) {
            Intent intent = new Intent(context, NodeTopicListActivity.class);
            Bundle bundle = new Bundle();
            intent.putExtra(KEY_NODE_INFO, node);
            context.startActivity(intent);
        } else {
            ToastUtils.show("节点错误");
        }
    }


}
